package com.tcs.server.teacher;

import com.google.common.collect.Collections2;
import com.google.common.collect.Lists;
import com.tcs.dao.teacher.TeacherDao;
import com.tcs.model.teachertest.*;
import com.tcs.util.uuid.UUIDUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

@Service(value="teacherService")
public class TeacherService implements  ITeacherService {

    private final Logger logger = LoggerFactory.getLogger(TeacherService.class);

    @Resource(name="teacherDao")
    private TeacherDao teacherDao;

    @Override
    public List<DisciplineBean> queryAllDisciplineBeanList() {
        return teacherDao.queryAllDisciplineBeanList();
    }

    @Override
    public List<DisciplineCategoryBean> queryDisciplineCategory(DisciplineBean disciplineBean) {
        List<DisciplineCategoryBean> disciplineCategoryResultl = null;
        try {
            disciplineCategoryResultl = teacherDao.queryDisciplineCategory(disciplineBean);
        } catch (Exception e) {
            logger.error("" , e);
        }
        return disciplineCategoryResultl;
    }

    @Override
    public List<YearSubjectBean> queryYearSubjectBean(DisciplineCategoryBean disciplineCategoryBean) {
        List<YearSubjectBean> yearSubjectList = null;
        try {
            yearSubjectList = teacherDao.queryYearSubjectBean(disciplineCategoryBean);
            for (YearSubjectBean yearSubjectBean : yearSubjectList) {
                yearSubjectBean.setDisciplineCategoryId(disciplineCategoryBean.getId());
            }
        } catch (Exception e) {
            logger.error("" , e);
        }
        return yearSubjectList;
    }

    @Override
    public List<TopicResultBean> queryTopicResultList(YearSubjectBean yearSubjectBean) {
        List<TopicResultBean> topicResultBeanList = null;
        try {
            topicResultBeanList = teacherDao.queryTopicResultList(yearSubjectBean);
        } catch (Exception e) {
            logger.error("" , e);
        }
        return topicResultBeanList;
    }

    @Override
    public List<SelectResultBean> querySelectResultList(TopicResultBean topicResultBean) {
        List<SelectResultBean> selectResultBeanList = null;
        try {
            selectResultBeanList = teacherDao.querySelectResultList(topicResultBean);
        } catch (Exception e) {
            logger.error("" , e);
        }
        return selectResultBeanList;
    }

    @Override
    public Integer getYearSubjectMaxId() {
        return teacherDao.getYearSubjectMaxId();
    }

    @Override
    public Integer getYearSubjectDisciplineCategory() {
        return teacherDao.getYearSubjectDisciplineCategory();
    }

    @Override
    public List<TopicResultBean> queryTopicListByTopicLike() {
        return teacherDao.queryTopicListByTopicLike();
    }

    @Override
    public Integer updateTopicBeanTopic(TopicBean topicBean) {
        return teacherDao.updateTopicBeanTopic(topicBean);
    }

    @Override
    @Transactional
    public boolean saveTopicBeanList(List<TopicBean> topicBeanList) {
        boolean result = true;
        List<TopicSelectBean> topicSelectBeanList = Lists.newArrayList();
        List<SelectBean> selectBeanList = Lists.newArrayList();
        TopicSelectBean topicSelectBean = null;
        if (!CollectionUtils.isEmpty(topicBeanList)) {
            for (TopicBean topicBean : topicBeanList) {
                List<SelectBean> selectBeans = topicBean.getSelectList();
                if (!CollectionUtils.isEmpty(selectBeans)) {
                    logger.debug("查看每次 selectBeans 集合中是否是空的 : {}" , selectBeans);
                    selectBeanList.addAll(selectBeans);
                    for (SelectBean selectBean : selectBeans) {
                        topicSelectBean = new TopicSelectBean(UUIDUtil.getUUID() , topicBean.getId() , selectBean.getId());
                        topicSelectBeanList.add(topicSelectBean);
                    }
                }
            }
            if (!CollectionUtils.isEmpty(selectBeanList) || !CollectionUtils.isEmpty(topicSelectBeanList)) {
                throw new NullPointerException("发现了集合是空的.");
            }
            logger.info("/- topicBeanList : {} \n  selectBeanList : {} \n  topicSelectBeanList : {} \n" , topicBeanList, selectBeanList , topicSelectBeanList);
            // 3次save
            teacherDao.saveTopicBeanList(topicBeanList);
            teacherDao.saveSelectBeanList(selectBeanList);
            teacherDao.saveTopicSelectBeanList(topicSelectBeanList);
        }
        return result;
    }
}
